package com.longcai.service.impl.clyjuser;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.longcai.bean.ApiResult;
import com.longcai.entity.customer.ClyjAddress;
import com.longcai.entity.customer.ClyjUser;
import com.longcai.entity.homepage.ClyjMenu;
import com.longcai.mapper.clyjuser.ClyjAddressMapper;
import com.longcai.service.clyjuser.ClyjAddressService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 * 用户地址表 服务实现类
 * </p>
 *
 * @author M
 * @since 2019-04-24
 */
@Service
public class ClyjAddressServiceImpl extends ServiceImpl<ClyjAddressMapper, ClyjAddress> implements ClyjAddressService {

    private Logger logger = LoggerFactory.getLogger(getClass());
    @Autowired
    private ClyjAddressMapper clyjAddressMapper;

    /**
     * 根据用户id查询对应收货地址
     *
     * @param id
     * @return
     */
    @Override
    public ApiResult getAds(String id) {
        try {
            //根据id查询有无对应的默认地址
            QueryWrapper<ClyjAddress> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("user_id", id);
            queryWrapper.eq("address_status","0");
            ClyjAddress clyjAddress = this.clyjAddressMapper.selectOne(queryWrapper);
            logger.debug("查询成功");
            return ApiResult.success(clyjAddress);
        } catch (Exception e) {
            logger.error("查询失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

    /**
     * 新增收货地址
     *
     * @param clyjAddress
     * @return
     */
    @Override
    public ApiResult saveAds(ClyjAddress clyjAddress) {
        ApiResult<Map> result = new ApiResult<Map>();
        try {
                //新增收货地址
                clyjAddress.setAddressId(UUID.randomUUID().toString().replace("-", ""));
                clyjAddress.setAddressStatus("1");
                Integer integer = this.clyjAddressMapper.insert(clyjAddress);
                //生成订单
                logger.debug("新增成功");
                return ApiResult.success(integer);
        } catch (Exception e) {
            logger.error("新增失败");
            e.printStackTrace();
            return ApiResult.fail("查询失败" + e.getMessage());
        }
    }

}
